import type { UserConfig, ConfigEnv, PluginOption } from 'vite'
import { loadEnv } from 'vite'
import { allPlugin } from './vite.plugin'

import { createProxy } from './proxy'
// ts中加载node核心模块需要安装node 的类型补充模块：yarn add -D @types/node
import path from 'path'
// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const env = loadEnv(mode, root)
  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = env
  const plugins:PluginOption| PluginOption[] = allPlugin(mode)
  return {
    base: VITE_PUBLIC_PATH,
    plugins,
    resolve: {
      alias: {
        '@': path.join(__dirname, 'src')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/styles/variables.scss";'
        }
      }
    },
    define: { // 去掉I18N tree shaking 警告
      __VUE_I18N_FULL_INSTALL__: true,
      __VUE_I18N_LEGACY_API__: false,
      __INTLIFY_PROD_DEVTOOLS__: false
    },
    server: {
      // Listening on all local IPs
      host: true,
      port: Number(VITE_PORT),
      // Load proxy configuration from .env
      proxy: createProxy(JSON.parse(VITE_PROXY))
    }
  }
}
